package trainning.metro.dao;

import trainning.metro.domain.Card;

import java.io.*;
import java.util.ArrayList;

public class MetroDao {
    private static ArrayList<String> stations = new ArrayList<>();
    private static ArrayList<Card> list = new ArrayList<>();
    //读取车站数据
    static {
        readStations(stations);
        readCard(list);
    }

    private static void readCard(ArrayList<Card> list) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("Day16\\src\\trainning\\metro\\dao\\user.txt"));
            ArrayList<String> user = new ArrayList<>();
            while (true) {
                String cardText = br.readLine();
                if(cardText != null){
                    user.add(cardText);
                }else {
                    break;
                }
            }
            for (String s : user) {
                String [] card = s.split(",");
                double money = Double.parseDouble(card[2]);
                Card c = new Card(card[0],card[1],money);
                list.add(c);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void readStations(ArrayList<String> stations) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("Day16\\src\\trainning\\metro\\dao\\station.txt"));
            String s = br.readLine();
            String [] split = s.split(",");
            for (int i = 0; i < split.length; i++) {
                stations.add(split[i]);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<String> getStations() {
        return stations;
    }

    public int isExist(String cardId) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Card card = list.get(i);
            if(card.getId().equals(cardId)){
                index = i;
                return index;
            }
        }
        return index;
    }

    public void saveUserLog(String log) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(
                    new FileOutputStream("Day16\\src\\trainning\\metro\\dao\\log.txt",true));
            byte [] bytes = log.getBytes();
            bos.write(bytes);
            bos.write("\r\n".getBytes());
            bos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean addUser(Card card) {
        list.add(card);
        saveUser(list);
        return true;
    }

    private void saveUser(ArrayList<Card> list) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(
                    new FileOutputStream("Day16\\src\\trainning\\metro\\dao\\user.txt"));
            for (Card card : list) {
                byte [] bytes = card.toString().getBytes();
                bos.write(bytes);
                bos.write("\r\n".getBytes());
                bos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean verify(String cardId, String x) {
        for (Card card : list) {
            //1.找到对应卡号
            if(card.getId().equals(cardId)){
                //2.确定第一个字符是否输入正确
                if(card.getUsername().startsWith(x)){
                    return true;
                }
            }
        }
        return false;
    }

    public String getUsername(String cardId) {
        for (Card card : list) {
            if(card.getId().equals(cardId)){
                String s = card.getUsername();
                String username = "*"+s.substring(1);
                return username;
            }
        }
        return null;
    }

    public double getMoney(String cardId) {
        for (Card card : list) {
            if(card.getId().equals(cardId)){
                return card.getMoney();
            }
        }
        return -1;
    }


    public boolean charge(String cardId, double money) {
        for (Card card : list) {
            if(card.getId().equals(cardId)){
                card.setMoney(money);
                saveUser(list);
                return true;
            }
        }
        return false;
    }
}
